<?php

ArrowController::importModel("templatesSystem.schemas.ArrowSchemaDescriptor");
ArrowController::importModel("templatesSystem.templates.ArrowTemplatesException");
/**
 * Arrow schema list
 * 
 * @version 1.0
 * @license  GNU GPL
 * @author Artur Kmera <artur.kmera@arrowplatform.org> 
 */
class ArrowSchemasList implements IArrowCacheable, IArrowSingleton{

	/**
	* Configuration from cache provider
	* 
	* @var Array
	*/
	private $configuration;
	
	/**
	 * File sources dir
	 *
	 * @var String
	 */
	private $sourcesDir;

	/**
	 * Instance
	 * 
	 * @var ArrowTemplateCompleter
	 */
	private static  $selfInstance = null;

	
	/**
	 * Singleton !NO_REMOTE
	 * 
	 * @return ArrowSchemasList Default instance
	 */
	public static function getDefault( ) {
		if (self :: $selfInstance == null ) {
			self :: $selfInstance = new ArrowSchemasList( );
		}
		return self :: $selfInstance;
	}
	
	/**
	 * Constructor !NO_REMOTE
	 * 
	 * @param Integer $projectId Project Id.
	 */
	private function __construct( ) {
		$this->sourcesDir = ArrowProject::PATH."/templatesSchemas";
		$confFile = ArrowProject::PATH. "/" . ArrowProject :: TEMPLATES_SCHEMAS_CONF;
		$cacheFile = ArrowCacheProvider::getFileCache( $this, $confFile, array( "file_prefix" => "schemas_list" ) );
		require_once $cacheFile;
		$this->configuration = $cache;
	}
	
	/**
	 * IArrowCacheable implementation
	 *
	 * @param Array $params
	 * @return Array
	 */
	public function generateCache($params){
		$project = ArrowController::getDefault()->getProject();
		$templatesStruct = $project->getTemplatesStructure();
		$tmp = array();
		$conf = $project->getXMLConfig(ArrowProject::TEMPLATES_SCHEMAS_CONF);
		$tmp["schemas"] = array();
		$tmp["membersConnections"] = array();
		foreach( $conf->schemas->schema as $schema ){
			$tmpSchema = array();
			$tmpSchema["name"] = (string) $schema["name"];
			$tmpSchema["dir"] = (string) $schema["dir"];
			$tmpSchema["description"] = (string) $schema->description;
			//$tmpSchema["members"] = array();
			foreach( $schema->templateMembers->templateMember as $member ){
				$id = $templatesStruct->getTemplateIdByMappingPath( (string) $member);
				//$tmpSchema["members"][] = $id;
				$tmp["membersConnections"][$id] = (int) $schema["id"];
			}
			//$packagesConf = $this->projectReference->getPackagesConf();
			
			$tmp["schemas"][ (string) $schema["id"] ] = $tmpSchema;
		}
		/*foreach($packagesConf as $package){
			$file = $package["dir"]."/schemas-list.xml";
			if(file_exists($file)){
				$doc = simplexml_load_file($file);
				foreach( $doc->schemas->schema as $schema ){
					foreach( $schema->templateMembers->templateMember as $member ){
						$id = $templatesStruct->getTemplateIdByMappingPath( (string) $member);
						$tmp["membersConnections"][$id] = (int) $schema["id"];
					}
				}
			}
			
		}*/
	
		return $tmp;
	}
	
	public function refresh(){
        $confFile = ArrowProject::PATH . "/" . ArrowProject :: TEMPLATES_SCHEMAS_CONF;
        clearstatcache();
		$cacheFile = ArrowCacheProvider::getFileCache( $this, $confFile, array( "file_prefix" => "schemas_list" ) );
		include $cacheFile;
		$this->configuration = $cache;
    
    }	

	
	/**
	 * Returns template by id
	 * 
	 * @param integer $templateId Template id
	 * @return ArrowTemplateDescriptor
	 */
	public function getSchemaById( $schemaId ){
		if (!isset ($this->configuration["schemas"][$schemaId]))
			throw new ArrowTemplatesException(array (
				"msg" => "Schema dosn't exists !.",
				"id" => $schemaId
				));
		$registred = $this->configuration["schemas"][$schemaId];
		$schema = new ArrowSchemaDesctiptor($this, $schemaId, $registred["name"], $this->sourcesDir . $registred["dir"], $registred["description"]);

		return $schema;
	}
	
	public function getSchemas(){ 
		$tmp = array();
		foreach($this->configuration["schemas"] as $id => $val){
			$tmp[] = $this->getSchemaById($id);
		}
		return $tmp;
	}
	
	public function getParentSchema( ArrowTemplateDescriptor $template ){
		$templateId = $template->getId();
		if( !isset( $this->configuration["membersConnections"][$templateId] ) )
			throw new ArrowTemplatesException( "Template '{$template}' don't have parent schema" );
		
		return $this->getSchemaById($this->configuration["membersConnections"][$templateId]);
	}
	
	/**
	 * Makes file structure for schema
	 *
	 * @param ArrowSchemaDesctiptor $schema
	 * //TODO Wykasowac calkiem ta funkcje i wdrozyc w web GUI
	 */
	public function makeFileStructure( $schema ){
		$blocks = $schema->getBlocksNames();

		if(!file_exists($schema->getDir()))
			mkdir( 	$schema->getDir(), 0777, true );

		foreach($blocks as $block){
			file_put_contents( 	$schema->getDir()."/".$block.".tpl", "Block: ".$block );
		}
	}
	

	
}
?>